<?php
//+--------------------------------------------
//|描述  task 任务投递   所有的异步任务
//|--------------------------------------------
//|Created by PhpStorm.
//| Author: DRG
//+-------------------------------------------
//|Date: 2018/6/6
//| Time: 8:37
//+--------------------------------------------
namespace app\common\lib\task;


use app\common\lib\ali\Sms;
use app\common\lib\Predis;
use app\common\lib\Redis;

class Task{
    
    /**
     * @param $data
     * @author DRG
     * 异步发送验证码
     */
    public function sendSms($data)
    {
        $data = $data['data'];
        try{
            $obj = new Sms();
            $resposer = $obj::sendSms($data['phone'], $data['code']);
        }catch (\Exception $e){
            //todo
            return false;
            //return app\common\lib\Util::show(config('code.empty'),'阿里大鱼内部异常');
        }
//        print_r($resposer);
        //发送 成功  写到redis   predis   同步
        if($resposer->Code === 'OK'){
            //redis
            
            $redis = Predis::getInstance()
                     ->set(Redis::smsKey($data['phone']), $data['code'], config('redis.expir'));
        }else{
            return false;
        }
        return true;
    }
    
    
    
    /**
     * 通过task机制发送赛况实时数据给客户端
     * @param $data
     * @param $serv swoole server对象
     */
    public function pushLive($data, $serv) {
        $clients = Predis::getInstance()->sMembers(config("redis.live_key"));
        foreach($clients as $fd) {
            $serv->push($fd, json_encode($data));
        }
    }
}